<?php
namespace app\admin\controller;
use app\admin\logic\OrderLogic;
use think\AjaxPage;
use think\console\command\make\Model;
use think\Page;
use think\Db;
use app\common\model\Community;
use app\common\model\Auths;
use app\common\model\Residents;
use app\common\model\Visitors;


class Auth extends Base {

    public function index(){
        return $this->fetch();
    }


    public function ajaxindex(){
        $condition = array();
        // $status = I('status',0);
        // $username = I('name');
        // $username ? $condition['username'] = ['like',"%$username%"] : false;
        // $condition['status'] = $status;
        $condition['level'] = 6;
        $community = new Community();
        $count = $community->where($condition)->count();
        $Page  = new AjaxPage($count,10);
        // $res = $community->where($condition)->field('id')->limit($Page->firstRow.','.$Page->listRows)->select();
        $data = D('community')->where($condition)->field('id')->limit($Page->firstRow.','.$Page->listRows)->select();
        $userList = [];
        foreach ($data as $key => $value) {
            foreach ($value as $k => $v) {
                $list = getpart($v);
                $userList[] = array(
                    'id' => $list[0]['id'],
                    'uid' => $list[0]['uid'],
                    'commun' => $this->getname($list),
                );
            }
        }
        $show = $Page->show();
        $this->assign('userList',$userList);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$Page);
        return $this->fetch();
    }


    public function getname($arr){
        for ($i=3; $i>=0; $i--) { 
            $name .= $arr[$i]['name'];
        }
        return $name;
    }


    public function add_edit(){
        if(IS_POST){
            $data = I('post.');
            $data['subTime'] = time();
            $id = $data['id'];
            if($id){
                $post = M('Auths')->where('id',$id)->save($data);
            }else{
                $post = M('Auths')->add($data);
            }
            if($post){
                $this->success('保存成功',U('Auth/index'));exit;
            }else{
                $this->error('保存失败',U('Auth/index'));exit;
            }
        }
        $id = I('id');
        if($id){
            $list = M('Auths')->where('id',$id)->find();
        }else{
            $list = [];
        }
        //  获取省份
        $level = !empty(I('levle')) ? I('levle') : 4;
        $com = M('Community')->where(array('parent_id'=>0,'level'=>$level))->select();
        $this->assign('com',$com);
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function del_com(){
        $id = I('id');
        $list = M('Community')->where('id',$id)->delete();
        if($list){
            $this->success('删除成功',U('Auth/index'));exit;
        }else{
            $this->error('删除失败',U('Auth/index'));exit;
        }
    }

    public function edit(){
        if(IS_POST){
            $file = request()->file('pic');
            if($file){
                $info = $file->rule('uniqid')->validate(['size'=>20480000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'upload/pic');
                if($info){
                    $pic = $info->getFilename(); 
                }else{
                    $this->success($file->getError(),U('Auth/edit'));exit;
                }
            }else{
                $pic = 'default.jpg';
            }
            $data = I('post.');
            $data['pic'] = $pic;
            $data['dz'] = getRegionName($data['province']).getRegionName($data['city']).getRegionName($data['district']);
            $data['subTime'] = time();
            $id = $data['id'];
            if(!$data['username'] || !$data['phone'] || !$data['card']){
                $this->error('信息不完整',U('Auth/edit'));exit;
            }
            if($id){
                $post = M('Auths')->where('id',$id)->save($data);
            }else{
                $post = M('Auths')->add($data);
            }
            if($post){
                $this->success('保存成功',U('Auth/detail'));exit;
            }else{
                $this->error('保存失败',U('Auth/edit'));exit;
            }
        }
        $id = I('id');
        if($id){
            $list = M('Auths')->where('id',$id)->find();
        }else{
            $list = [];
        }
        //  获取省份
        $province = M('region')->where(array('parent_id'=>0,'level'=>1))->select();
        $this->assign('province',$province);
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function detail(){
        return $this->fetch();
    }


    public function ajaxdetail(){
        $condition = array();
        $username = I('name');
        $status = I('status',0);
        $username ? $condition['username'] = ['like',"%$username%"] : false;
        $condition['status'] = $status;
        $ModelAuths = new Auths();
        $count = $ModelAuths->where($condition)->count();
        $Page  = new AjaxPage($count,10);
        $userList = $ModelAuths->where($condition)->limit($Page->firstRow.','.$Page->listRows)->select();
        $show = $Page->show();
        $this->assign('userList',$userList);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$Page);
        return $this->fetch();
    }



    public function residents(){
        return $this->fetch();
    }


    public function ajaxresidents(){
        $condition = array();
        $ModelResidents = new Residents();
        $count = $ModelResidents->where($condition)->count();
        $Page  = new AjaxPage($count,10);
        $userList = $ModelResidents->where($condition)->limit($Page->firstRow.','.$Page->listRows)->select();
        $show = $Page->show();
        $this->assign('userList',$userList);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$Page);
        return $this->fetch();
    }



    public function add_residents(){
        if(IS_POST){
            $file = request()->file('pic');
            if($file){
                $info = $file->rule('uniqid')->validate(['size'=>20480000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'upload/pic');
                if($info){
                    $pic = $info->getFilename(); 
                }else{
                    $this->success($file->getError(),U('Auth/add_residents'));exit;
                }
            }else{
                $pic = 'default.jpg';
            }
            $data = I('post.');
            $data['pic'] = $pic;
            $data['subTime'] = time();
            $id = $data['id'];
            if(!$data['rname'] || !$data['phone'] || !$data['card']){
                $this->error('信息不完整',U('Auth/add_residents'));exit;
            }
            if($id){
                $post = M('Residents')->where('id',$id)->save($data);
            }else{
                $post = M('Residents')->add($data);
            }
            if($post){
                $this->success('保存成功',U('Auth/residents'));exit;
            }else{
                $this->error('保存失败',U('Auth/add_residents'));exit;
            }
        }
        $id = I('id');
        if($id){
            $list = M('Residents')->where('id',$id)->find();
        }else{
            $list = [];
        }
        
        $this->assign('list',$list);

        return $this->fetch();
    }


    public function del_residents(){
        $id = I('id');
        $list = M('Residents')->where('id',$id)->delete();
        if($list){
            $this->success('删除成功',U('Auth/residents'));exit;
        }else{
            $this->error('删除失败',U('Auth/residents'));exit;
        }
    }



    public function visitors(){
        return $this->fetch();
    }


    public function ajaxvisitors(){
        $condition = array();
        $name = I('name');
        $name ? $condition['name'] = ['like',"%$name%"] : false;
        $ModelVisitors = new Visitors();
        $count = $ModelVisitors->where($condition)->count();
        $Page  = new AjaxPage($count,10);
        $userList = $ModelVisitors->where($condition)->limit($Page->firstRow.','.$Page->listRows)->select();
        $show = $Page->show();
        $this->assign('userList',$userList);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$Page);
        return $this->fetch();
    }



    public function add_visitors(){
        if(IS_POST){
            $file = request()->file('pic');
            if($file){
                $info = $file->rule('uniqid')->validate(['size'=>20480000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'upload/pic');
                if($info){
                    $pic = $info->getFilename(); 
                }else{
                    $this->success($file->getError(),U('Auth/add_visitors'));exit;
                }
            }else{
                $pic = 'default.jpg';
            }
            $data = I('post.');
            $data['pic'] = $pic;
            $data['subTime'] = time();
            $id = $data['id'];
            if(empty($data['name']) || empty($data['phone']) || empty($data['address']) || empty($data['aname'])){
                $this->error('信息不完整',U('Auth/add_visitors'));exit;
            }
            if($id){
                $post = M('Visitors')->where('id',$id)->save($data);
            }else{
                $post = M('Visitors')->add($data);
            }
            if($post){
                $this->success('保存成功',U('Auth/visitors'));exit;
            }else{
                $this->error('保存失败',U('Auth/add_visitors'));exit;
            }
        }
        $id = I('id');
        if($id){
            $list = M('Visitors')->where('id',$id)->find();
        }else{
            $list = [];
        }
        
        $this->assign('list',$list);
        return $this->fetch();
    }


    public function del_visitors(){
        $id = I('id');
        $list = M('Visitors')->where('id',$id)->delete();
        if($list){
            $this->success('删除成功',U('Auth/visitors'));exit;
        }else{
            $this->error('删除失败',U('Auth/visitors'));exit;
        }
    }


    
}